package com.tencent.servlet;

import com.tencent.myssm.ViewBaseServlet;
import com.tencent.pojo.Student;
import com.tencent.service.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author: Fame
 * @date: 2022-06-20 22:44
 * @description:
 */
@WebServlet("/student")
public class StudentServlet extends ViewBaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer pageNo = 1;
        String pageStr = req.getParameter("pageNo");
        if (pageStr != null && pageStr != "" ){
             pageNo = Integer.valueOf(pageStr);
        }

        HttpSession session1 = req.getSession();
        session1.setAttribute("pageNo",pageNo);
        StudentServiceImpl service = new StudentServiceImpl();
        // 总记录条数
        int count = service.getStuCount();
        // 总记录页数
        int pageEnd = (count+5-1)/5;

        List<Student> list = service .get(pageNo);//查询到list数据
        HttpSession session = req.getSession(); //获取session会话
        session.setAttribute("studentList" ,list);//设置Attribute
        session.setAttribute("pageEnd",pageEnd);
        super.processTemplate("student",req,resp);//重定向到父类
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
